#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/ioctl.h>


#define FIFO_CLEAR 0x1
#define BUFFER_LEN 20 
#define FILE_DEVICE "/dev/globalfifo"
void main(void)
{
    int fd,num;
    char rd_ch[BUFFER_LEN];
    fd_set rfds,wfds;

    fd = open(FILE_DEVICE,O_RDONLY | O_NONBLOCK);
    if(fd != -1){
        if(ioctl(fd,FIFO_CLEAR,0) < 0){
            printf("ioctl command failed\n");
        }

        while(1){
            FD_ZERO(&rfds);
            FD_ZERO(&wfds);
            FD_SET(fd,&rfds);
            FD_SET(fd,&wfds);

/*extern int select (int __nfds, 
          fd_set *__restrict __readfds,
		   fd_set *__restrict __writefds,
		   fd_set *__restrict __exceptfds,
		   struct timeval *__restrict __timeout);
*/
            select(fd+1,&rfds,&wfds,NULL,NULL);//查询文件状态，最终调用到 驱动的poll 函数
            /*file can read*/
            if(FD_ISSET(fd,&rfds)){
                printf("poll monitor:can be read\n");
            }
            /*file can write*/
            if(FD_ISSET(fd,&wfds)){
                printf("poll monitor:can be written\n");
            }    
            sleep(2);
        }
    }else {
        printf("Device open failed\n");
    }
}